package com.pid.customerflow.service.impl;

import java.util.List;
import com.pid.common.utils.DateUtils;
import com.pid.common.utils.SecurityUtils;
import com.pid.common.utils.StringUtils;
import com.pid.common.utils.uuid.UUID;
import com.pid.system.service.ISysUserService;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pid.customerflow.mapper.WorkflowBigcustomerMapper;
import com.pid.customerflow.domain.WorkflowBigcustomer;
import com.pid.customerflow.service.IWorkflowBigcustomerService;
import org.activiti.engine.task.Task;
import org.activiti.engine.TaskService;
import java.util.stream.Collectors;
/**
 * 大客户流程Service业务层处理
 * 
 * @author pid
 * @date 2022-03-01
 */
@Service
public class WorkflowBigcustomerServiceImpl implements IWorkflowBigcustomerService 
{
    @Autowired
    private WorkflowBigcustomerMapper workflowBigcustomerMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ProcessRuntime processRuntime;
    @Autowired
    private TaskService taskService;
    /**
     * 查询大客户流程
     * 
     * @param id 大客户流程主键
     * @return 大客户流程
     */
    @Override
    public WorkflowBigcustomer selectWorkflowBigcustomerById(String id)
    {
        return workflowBigcustomerMapper.selectWorkflowBigcustomerById(id);
    }

    /**
     * 查询大客户流程列表
     * 
     * @param workflowBigcustomer 大客户流程
     * @return 大客户流程
     */
    @Override
    public List<WorkflowBigcustomer> selectWorkflowBigcustomerList(WorkflowBigcustomer workflowBigcustomer)
    {
        // workflow_bigcustomer表中当前登录用户部门下所有用户创建的workflow_bigcustomer
        return workflowBigcustomerMapper.selectWorkflowBigcustomerListByWorkflowBigcustomerAndDeptId(workflowBigcustomer,SecurityUtils.getLoginUser().getUser().getDeptId());
    }

    /**
     * 查询请假列表带任务状态
     *
     * @param workflowBigcustomer 请假
     * @return 请假
     */
    @Override
    public List<WorkflowBigcustomer> selectWorkflowBigcustomerAndTaskNameList(WorkflowBigcustomer workflowBigcustomer) {
        List<WorkflowBigcustomer> workflowLeaves = workflowBigcustomerMapper.selectWorkflowBigcustomerList(workflowBigcustomer);
        List<String> collect = workflowLeaves.parallelStream().map(wl -> wl.getInstanceId()).collect(Collectors.toList());
        if(collect!=null&&!collect.isEmpty()) {
            List<Task> tasks = taskService.createTaskQuery().processInstanceIdIn(collect).list();
            workflowLeaves.forEach(
                    wl->{
                        Task task = tasks.parallelStream().filter(t -> t.getProcessInstanceId().equals(wl.getInstanceId())).findAny().orElse(null);
                        if (task != null) {
                            wl.setTaskName(task.getName());
                        }
                    }
            );
        }
        return workflowLeaves;
    }

    /**
     * 新增大客户流程
     * 
     * @param workflowBigcustomer 大客户流程
     * @return 结果
     */
    @Override
    public int insertWorkflowBigcustomer(WorkflowBigcustomer workflowBigcustomer)
    {
        // id是唯一的，用来表示每一个不同的请假流程
        // id: 请假流程启动一个流程实例，就可以将请假单的 id 作为业务标识存储到 activiti 中，
        // 将来查询 activiti 的流程实例信息就可以获取请假单的id 从而关联查询业务系统数据库得到请假单信息。
        String id = UUID.randomUUID().toString();  // 此id对应act_ru_execution表中的BUSINESS_KEY_
        workflowBigcustomer.setId(id);
        workflowBigcustomer.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录部门的所有部门经理的用户名称，以便后续发送给他们处理代办任务
        String join = StringUtils.join(sysUserService.selectUserNameByPostCodeAndDeptId("se", SecurityUtils.getLoginUser().getUser().getDeptId()), ",");
        ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
                .start()
                .withProcessDefinitionKey("version1")// 使用哪一个流程作为当前的进度管理流程
                .withName(workflowBigcustomer.getCustomerName())
                .withBusinessKey(id)
                .build());
        workflowBigcustomer.setInstanceId(processInstance.getId());
        workflowBigcustomer.setState("0");
        workflowBigcustomer.setDelFlag("0");
        workflowBigcustomer.setCreateName(SecurityUtils.getNickName());
        workflowBigcustomer.setCreateBy(SecurityUtils.getUsername());
        workflowBigcustomer.setCreateTime(DateUtils.getNowDate());
        return workflowBigcustomerMapper.insertWorkflowBigcustomer(workflowBigcustomer);
    }

    /**
     * 修改大客户流程
     * 
     * @param workflowBigcustomer 大客户流程
     * @return 结果
     */
    @Override
    public int updateWorkflowBigcustomer(WorkflowBigcustomer workflowBigcustomer)
    {
        workflowBigcustomer.setUpdateTime(DateUtils.getNowDate());
        return workflowBigcustomerMapper.updateWorkflowBigcustomer(workflowBigcustomer);
    }

    /**
     * 批量删除大客户流程
     * 
     * @param ids 需要删除的大客户流程主键
     * @return 结果
     */
    @Override
    public int deleteWorkflowBigcustomerByIds(String[] ids)
    {
        return workflowBigcustomerMapper.deleteWorkflowBigcustomerByIds(ids);
    }

    /**
     * 删除大客户流程信息
     * 
     * @param id 大客户流程主键
     * @return 结果
     */
    @Override
    public int deleteWorkflowBigcustomerById(String id)
    {
        return workflowBigcustomerMapper.deleteWorkflowBigcustomerById(id);
    }
}
